<?php

namespace pam\Controller;

/**
 * Description of SocialIndicatorsController
 *
 * @author Robert Bwana <robert.bwana@ssra.go.tz>
 */

use Zend\Mvc\Controller\AbstractActionController as AAC;
use Zend\View\Model\ViewModel;
use Zend\Form\FormInterface;
use pam\Form\IndicatorForm;
use pam\Model\Indicator;
use pam\Service\PolicyServiceInterface as PSI;
use pam\Service\IndicatorsServiceInterface as ISI;

class SocialIndicatorsController extends AAC{
    protected $indicatorService;
    protected $form;
    protected $policyService;
    protected $id;
    
    public function __construct(ISI $indicatorService,PSI $policyService, FormInterface $form) {
        $this->indicatorService = $indicatorService;
        $this->policyService = $policyService;
        $this->form = $form;    
    }
    
    public function viewAction() {
        $this->id = $this->params('id');
        return new ViewModel(array(         
            'policy' => $this->policyService->findPolicy($this->id),
            'indicators' => $this->indicatorService->findAllInPolicy($this->id),
        ));
    }
    
    public function addAction() {
        $request = $this->getRequest();

        if ($request->isPost()) {
            $this->form->setData($request->getPost());
//            $form = $this->form->get('indicator-fieldset');
//        $form->get('policy_id')->setValue($this->id);
            
            if ($this->form->isValid()) {
                //try {
                   // \Zend\Debug\Debug::dump($this->form->getData());die('end');
                    $this->indicatorService->saveIndicator($this->form->getData());

                    return $this->redirect()->toRoute('social-indicators', array('action' => 'view' ),true);
//                } catch (\Exception $e) {
//                    die('Controller error:' . $e->getMessage());
//                }
            }
        }

        return new ViewModel(array(
            'form' => $this->form,
            'id' => $this->params('id'),
        ));
    }
    
    public function editAction() {
         $request = $this->getRequest();
         try{
         $indicator = $this->indicatorService->findIndicator($this->params('ind_id'));
         }  catch (\InvalidArgumentException $e){
             die($e->getMessage());
         }
         $this->form->bind($indicator);
         if ($request->isPost()) {
             
             $this->form->setData($request->getPost());
             
             if ($this->form->isValid()) {
                 try {
                     
                     $this->indicatorService->saveIndicator($indicator); 

                     return $this->redirect()->toRoute('social-indicators', array('action' => 'view' ),true);
                 } catch (\Exception $e) {
                     die($e->getMessage());
                     // Some DB Error happened, log it and let the user know
                 }
             }
         }

         return new ViewModel(array(
             'form' => $this->form
         ));
        
    }
    
    public function deleteAction() {
        try {
             $indicator = $this->indicatorService->findIndicator($this->params('ind_id'));
         } catch (\InvalidArgumentException $e) {
             return $this->redirect()->toRoute('social-indicators');
         }

         $request = $this->getRequest();

         if ($request->isPost()) {
             $del = $request->getPost('delete_confirmation', 'No');

             if ($del === 'Yes') {
                 $this->indicatorService->deleteIndicator($indicator);
             }

             return $this->redirect()->toRoute('social-indicators', array('action' => 'view' ),true);
         }

         return new ViewModel(array(
             'indicator' => $indicator
         ));
        
    }
}
